# -*- coding: utf-8 -*-
import scrapy
from homework.items import BookItem

class DangdangSpider(scrapy.Spider):
    name = 'dangdang'
    allowed_domains = ['dangdang.com']
    start_urls = ['http://search.dangdang.com/?key=python&SearchFromTop=1&catalog=&page_index=1']
    p=1

    def parse(self, response):
        #pass
        res = response.selector.css("ul.bigimg li")
        for vo in res:
            item = BookItem()
            if vo.css("a img::attr(src)").extract_first() == 'images/model/guan/url_none.png':
                #print(vo.css("a img::attr(data-original)").extract_first())
                item['img_url'] = vo.css("a img::attr(data-original)").extract_first()
            else:
                item['img_url'] = vo.css("a img::attr(src)").extract_first()
            item['title'] = vo.css("p.name a::attr(title)").extract_first()
            item['price'] = vo.css("p.price span.search_now_price::text").extract_first().replace('¥','')
            #print(vo.css("p.price span.search_now_price::text").extract_first().replace('¥',''))
            item['author'] = vo.css("p.search_book_author").re_first('<a.*?>(.*?)</a>')
            yield item

        self.p += 1
        if self.p <= 10:
            next_url = 'http://search.dangdang.com/?key=python&SearchFromTop=1&catalog=&page_index=' + str(self.p)
            url = response.urljoin(next_url)
            yield scrapy.Request(url=url,callback = self.parse)
